<?php

ini_set('memory_limit', '800M');

$appRoot = dirname(__FILE__);

set_include_path(get_include_path() . PATH_SEPARATOR . $appRoot);
set_include_path(get_include_path() . PATH_SEPARATOR . $appRoot . '/lib');

define('MORIARTY_ARC_DIR', $appRoot . '/lib/arc'.DIRECTORY_SEPARATOR);
define('MORIARTY_DIR', $appRoot . '/lib/moriarty'.DIRECTORY_SEPARATOR);
define('WSR_DIR', $appRoot . '/lib/wsr'.DIRECTORY_SEPARATOR);

require_once MORIARTY_ARC_DIR . 'ARC2.php';
require_once MORIARTY_DIR .'moriarty.inc.php';
require_once MORIARTY_DIR . 'store.class.php';
require_once WSR_DIR . 'axgraph.class.php';
require_once WSR_DIR . 'activation.class.php';
require_once WSR_DIR . 'rules.class.php';
require_once WSR_DIR . 'triplecache.class.php';
require_once WSR_DIR . 'largescalespreadingactivation.class.php';
require_once WSR_DIR . 'experiment.class.php';

/*
$query = 'describe ?s where { ?s a <http://purl.org/ontology/po/Episode> } LIMIT 10';

$store_uri = 'http://api.talis.com/stores/bbc-backstage';
$store = new Store($store_uri);

//$response =  $store->get_sparql_service()->graph($query);

//echo $response->body;


$graph =  $store->get_sparql_service()->graph_to_simple_graph($query);

*/

$test_data = array();

$test_data['EARLY'] = array( 'TO_LOAD' => array ( "http://purl.org/ontology/po/Episode" ) );

$test_data['ORIGINAL'] = array(
				'TO_LOAD' => array( "http://www.bbc.co.uk/programmes/b00hxnfs#programme" ),  // original query
				'TRACE'   => array("http://www.bbc.co.uk/programmes/formats/animation#format" )
				);

$test_data['QUEEN2'] = array(
				'TO_LOAD' => array ( "http://www.bbc.co.uk/music/artists/8201ee6e-a7d8-4e10-aebd-60699d7e91cf#artist",
				                     "http://www.bbc.co.uk/music/artists/a0b77a10-ecff-4b84-b1f3-008391038d67#artist" )
				);
$test_data['QUEEN1'] = array(
				'TO_LOAD' => array ( "http://www.bbc.co.uk/music/artists/a0b77a10-ecff-4b84-b1f3-008391038d67#artist" )
				);


function get_graph_from_triple_cache($to_load) {

	$triple_cache = new TripleCache();
	
	$triple_cache->update($to_load);
	
	$graph = $triple_cache->get_graph();
	
	//echo "=================\n";
	//print_r( $graph );
	//echo "=================\n";
	
	return $graph;

}

function test_activate_with_triple_cache($to_load,$trace) {
	$graph = get_graph_from_triple_cache($to_load);
	test_activate($graph,$to_load,$trace);
}

function test_activate_with_turtle($filename,$to_load,$trace) {
	$turtle = file_get_contents($filename);
	echo "$filename => " . strlen($turtle) . " chars\n";
	$graph = new SimpleGraph();
	$graph->from_turtle($turtle);
	$t = $graph->get_triples();
	echo "got " . count($t) . " triples \n";
	$activation = test_activate($graph,$to_load,$trace);
	$g = $activation->get_graph();
	$t = $g->get_triples();
	echo "got " . count($t) . " triples \n";
	$act = $activation->activation_levels;
	asort($act,SORT_NUMERIC);
	$act = array_reverse($act,true);
	foreach ( $act as $ent => $ent_act ) {
		echo $incoming . "\t" . $ent . "\n";
	}

}

// test_activate_with_turtle("big-turtle.txt",$to_load,$trace);



function test_activate($graph,$initial_entites,$trace) {
	
	$axgraph = new AxGraph($graph);
	
	//$t = $axgraph->get_triples();
	//echo "test_activate: axgraph has " . count($t) . " triples \n";
	
	//$t2 = array_slice($t,0,10);
	//print_r($t2); echo "\n\'";
	
	//$initial_entites = array( "http://purl.org/ontology/po/Episode" );
	$initial_entites = array( "http://www.bbc.co.uk/programmes/b00hqhj2#programme" );
	
	
	$activation = new Activation();
	$activation->set_graph($axgraph);
	
	$g = $activation->get_graph();
	$t = $g->get_triples();
	echo "test_activate: activation->get_graph has " . count($t) . " triples \n";

	$activation->set_initial_activated_entities( $initial_entites );
	$activation->do_steps(5);
	
	//print_r( $activation->in );
	//print_r( $activation->activation_levels );
	
	$to_load = $activation->get_loadable_entities();
	
	//echo "=================\n";
	//print_r( $to_load );
	//echo "=================\n";
	
	return $activation;
}

function test_lisa($to_load) {

	$lisa = new LargeScaleSpreadingActivation();
	
	$lisa->nos_internal_cycles = 5; 
	$lisa->use_meta = true;
	$lisa->set_load_threshold( 1 );
	$lisa->set_spread_factor( 0.4 );
	$lisa->clear();
	
	$lisa->activate($to_load);
	
	$lisa->iterate(2);
	
	$in = $lisa->activation->in;
	asort($in,SORT_NUMERIC);
	//$in = array_reverse($in,true);
	foreach ( $in as $ent => $incoming ) {
		echo $lisa->activation->activation_levels[$ent] . "\t" . $incoming . "\t" . $ent . "\n";
	}

}


//$dataset = 'ORIGINAL';
//$dataset = 'QUEEN1';
$dataset = 'QUEEN2';

$to_load = $test_data[$dataset]['TO_LOAD'];
$to_trace = $test_data[$dataset]['TRACE'];

run_experiment($dataset,$to_load,$to_trace);

function run_experiment($dataset,$to_load,$to_trace=false) {

	$lisa = new LargeScaleSpreadingActivation();
	
	$lisa->nos_internal_cycles = 5; 
	$lisa->use_meta = true;
	$lisa->set_load_threshold( 1 );
	$lisa->set_spread_factor( 0.2 );
	$lisa->clear();
	
	//$thresholds = array(1);
	//$thresholds = array(0,1,2,5);
	//$thresholds = array(0.2);
	$thresholds = array(0.01,0.02,0.05,0.1,0.2,0.5,1,2,5);
	
	$experiment = new Experiment($lisa);
	
	//$experiment->nos_load_iterations = 3;
	
	$experiment->run($to_load, $thresholds);
	
	$delim = ","; $ext="csv";
	
	$data = $experiment->print_stats(true,$delim);
	
	//echo $data;
	
	$graph = $experiment->lisa->activation->get_graph();
	
	// print_r($graph);
	
	//$t = $graph->get_triples();
	//echo "run_experiment: graph has " . count($t) . " triples \n";
	
	//$t2 = array_slice($t,0,10);
	//print_r($t2); echo "\n\n\n";

	
	/*
	$turtle = $graph->to_turtle();
	
	//echo $turtle . "\n";
	
	$outfile = "big-turtle.txt";
	
	file_put_contents($outfile,$turtle);
	
	echo "output sent to $outfile \n";
	*/
	
	$outfile = "output-" . time() . "." . $ext;
	
	$param_info = $dataset . $delim . "spread factor" . $delim . $lisa->get_spread_factor();
	
	if ( $lisa->use_fanout ) {
		$param_info .=  $delim . "fanout ratio". $delim . $experiment->fanout_ratio;
	}
	
	if ( $lisa->use_meta ) {
		$param_info .=  $delim . "using meta";
	}
	
	$header  =  $param_info . "\n\n";
	
	file_put_contents($outfile,$header . $data);
	
	echo $param_info . "\n";
	echo "output sent to $outfile \n";
	

}


?>